package com.sunwayworld.basemodule.coredata.product.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.sunwayworld.framework.exception.InvalidDataException;
import com.sunwayworld.framework.i18n.I18nHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.coredata.product.dao.LimsProductDao;
import com.sunwayworld.basemodule.coredata.product.bean.LimsProductBean;
import com.sunwayworld.basemodule.coredata.product.service.LimsProductService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Repository
@GikamBean
public class LimsProductServiceImpl implements LimsProductService {

    @Autowired
    private LimsProductDao limsProductDao;

    @Override
    @SuppressWarnings("unchecked")
    public LimsProductDao getDao() {
        return limsProductDao;
    }

    @Override
    @Transactional
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsProductBean productBean = jsonWrapper.parseUnique(LimsProductBean.class);
        String chooseMapJson = productBean.getExt$Item("choosemap");
        List<LimsProductBean> productBeans = JSONArray.parseArray(chooseMapJson, LimsProductBean.class);
        List<Long> ids = ApplicationContextHelper.getNextIdentityList(productBeans.size());
        List<String> sampleTypeCodeList = productBeans.stream().map(LimsProductBean::getSampleTypeCode).collect(Collectors.toList());
        productBeans.forEach(pb -> {
            BeanUtils.copyProperties(productBean, pb, "sampleTypeCode","sampleTypeName");
            pb.setId(ids.remove(0));
        });
        List<LimsProductBean> productBeanList = getDao().selectListByOneColumnValue(productBean.getProductMainId(), "productMainId");
        if (!productBeanList.isEmpty()){
            Optional<LimsProductBean> any = productBeanList.stream().filter(p -> productBean.getEtCode().equals(p.getEtCode()) && sampleTypeCodeList.contains(p.getSampleTypeCode())).findAny();
            if (any.isPresent()){
                List<LimsProductBean> beans = productBeanList.stream().filter(p -> productBean.getEtCode().equals(p.getEtCode()) && sampleTypeCodeList.contains(p.getSampleTypeCode())).collect(Collectors.toList());
                String sampleStr = beans.stream().map(LimsProductBean::getSampleTypeName).collect(Collectors.joining("<br>"));
                throw new InvalidDataException(I18nHelper.getMessage("FOLLOWING.SAMPLE.EXISTS.PROGRAM") +"<br/>"+sampleStr + "<br/>"+ I18nHelper.getMessage("TEMPLATE.HAVED.CONMAIN"));
            }
        }
        getDao().insert(productBeans);
        return productBeans.get(0).getId();
    }

}
